#!/usr/bin/env bash

index()
{
    local split="----------------------------------------"

    echo "$FUNCNAME"
    echo $split

    local -a arr        # 索引数组

    arr[${#arr[@]}]=100
    arr[${#arr[@]}]=200
    echo ${#arr[@]}
    if [[ 100 in $[arr[@]} ]]
    then
        echo "100 in ${arr[@]}"
    fi

    echo

    arr=($(seq 10))
    echo "seq 10: ${arr[*]}"

    echo

    local up=10
    eval arr=({1..$up})     # 两次替换需要eval
    echo "{1..10}: ${arr[*]}"
    eval local -a arr2=({1..$up})
    for n in ${arr2[*]}
    do
        echo $n
    done

    echo

    echo "for n in {1..10..2}"
    for n in {1..10..2}
    do
        echo $n
    done

    echo $split

    arr=([2]=100 [3]=200)
    arr[0]="aaa"
    arr[1]="bbb"
    arr[4]="ccc ddd"
    arr[10]="xxx yyy"
    arr+=("111 222")
    declare -p arr

    echo $split

    echo "元素个数: ${#arr[*]}"

    echo $split

    echo "@ * 区别"
    echo

    echo '${arr[*]}: '${arr[*]}
    for i in ${arr[*]}
    do
        echo "    $i"
    done
    echo

    OIFS="$IFS"
    IFS="|"
    echo "IFS --> |"
    echo '"${arr[*]}": 按照IFS组合成："$1|$2|$3|...|$n"'
    echo "${arr[*]}"
    IFS="$OIFS"
    echo

    echo '${arr[@]}: '${arr[@]}
    for i in ${arr[@]}
    do
        echo "    $i"
    done
    echo

    echo '"${arr[@]}": 等同于"$1" "$2" "$3" ... "$n"'
    for i in "${arr[@]}"
    do
        echo "    $i"
    done

    echo $split

    echo '${arr[*]} --- 错误'
    for i in ${arr[*]}
    do
        echo $i
    done

    echo $split

    echo '${arr[@]} --- 错误，与*一样'
    for i in ${arr[@]}
    do
        echo $i
    done

    echo $split

    echo '"${arr[@]}" --- 正确'
    for i in "${arr[@]}"
    do
        echo $i
    done

    echo $split

    echo '${#arr[*]} --- 错误，下标不一定连续'
    for (( i=0; i<${#arr[*]}; i++ ))
    do
        echo ${arr[$i]}
    done

    echo $split

    echo '${!arr[*]} -- 正确'
    for k in ${!arr[*]}
    do
        echo ${arr[$k]}
    done

    echo $split

    echo "数组复制"

    echo '("${arr[@]}") --- 下标重排，不完全正确'
    local -a arr_copy=("${arr[@]}")
    declare -p arr_copy

    echo

    echo 'for i in ${!arr[@]} --- 正确'
    local -a arr_copy2
    for i in ${!arr[@]}
    do
        arr_copy2[$i]=${arr[$i]}
    done
    declare -p arr_copy2
}

asso()
{
    local split="----------------------------------------"

    echo "$FUNCNAME"
    echo $split

    local k
    local v

    local -A arr     # -A 关联数组

    arr=(['c']=100 ['d']=200)
    arr['a']="hello"
    arr['b']="world"
    declare -p arr
    echo
    for v in ${arr[*]}
    do
        echo $v
    done
    echo
    for k in ${!arr[*]}
    do
        echo "$k => ${arr[$k]}"
    done

    echo $split

    arr=([hello]=world [a b]='c d')
    declare -p arr
    echo
    echo '${!arr[*]} --- 错误，空格被当成分隔符，等同于${!array[@]}'
    for k in ${!arr[*]}      # "hello" "a" "b"
    do
        echo $k
    done
    echo
    echo '"${!arr[*]}" --- 错误，拼接成一个字符串'
    for k in "${!arr[*]}"      # "hello a b"
    do
        echo $k
    done
    echo
    echo '"${!arr[@]}" --- 正确'
    for k in "${!arr[@]}"      # "hello" "a b"
    do
        echo $k
    done
    echo
    echo '${arr[*]} --- 错误，等同于${array[*]}'
    for v in ${arr[*]}         # "c" "d" "world"
    do
        echo $v
    done
    echo
    echo '"${arr[*]}" --- 错误'
    for v in "${arr[*]}"       # "c d world"
    do
        echo $v
    done
    echo
    echo '"${arr[@]}" --- 正确'
    for v in "${arr[@]}"       # "c d" "world"
    do
        echo $v
    done

    echo $split

    echo "数组复制"

    echo 'for i in "${!arr[@]}" --- 正确'
    local -A arr_copy2
    for i in "${!arr[@]}"
    do
        arr_copy2[$i]=${arr[$i]}
    done
    declare -p arr_copy2
}

readin()
{
    local split="----------------------------------------"

    echo "$FUNCNAME"
    echo $split

    local -a arr
    echo "0 10 20 30 40 50" >array.tmp
    read -a arr <array.tmp
    echo ${arr[*]}
    for i in ${arr[*]}
    do
        echo $i
    done

    echo $split

    local s="a|b|c|d|e|f"
    IFS="|" read -a arr <<<$s   # read前可以临时设置IFS
    echo ${arr[*]}
    for i in ${arr[*]}
    do
        echo $i
    done
}

query()
{
    local split="----------------------------------------"

    echo "$FUNCNAME"
    echo $split

    local -A arr=(
            [a a a]=1
            [b b b]=1
            [c c c]=1
            )
    k='a a a'
    if (( arr[$k] == 1 ))
    then
        echo "($k) in array"
    else
        echo "($k) not in array"
    fi
    k='d d d'
    if (( arr[$k] == 1 ))
    then
        echo "($k) in array"
    else
        echo "($k) not in array"
    fi
    for k in "${!arr[@]}"
    do
        echo $k
    done
    for i in "${arr[@]}"
    do
        echo $i
    done
    if (( ${#arr[@]} > 2 ))
    then
        echo "array size(${#arr[@]}) > 2"
    fi
}

split_join()
{
    local split="----------------------------------------"

    echo "$FUNCNAME"
    echo $split

    OIFS="$IFS"

    local s="/aaa//bbb/ ccc cc / ddd/"
    echo "original: $s"

    echo

    IFS=/
    local -a arr=($s)
    echo "split:"
    declare -p arr
    IFS="$OIFS"

    echo

    local s2=""
    for i in "${arr[@]}"
    do
        s2+=$i":"
    done
    s2=${s2:0:-1}
#    s2=${s2:0:${#s2}-1}
    echo "join: $s2"
}

multi()
{
    local split="----------------------------------------"

    echo "$FUNCNAME"
    echo $split

    echo "合成（仅测试，数组不支持多级结构）"
    echo
    local -a conn
    local -A config
    local s

    config=(
            [host]=host1
            [port]=3306
            [database]=db1
            )
    declare -p config
    s=""
    for k in ${!config[*]}
    do
        s+="[$k]=${config[$k]} "
    done
    conn[1]=$s
    config=(
            [host]=host2
            [port]=3307
            [database]=db2
            )
    declare -p config
    s=""
    for k in ${!config[*]}
    do
        s+="[$k]=${config[$k]} "
    done
    conn[2]=$s

    for i in ${!conn[*]}
    do
        echo "  ${conn[$i]}"
    done

    echo $split

    echo "解析"
    echo
    local -a db
    db[1]=$(cat <<EOF
        [username]=dbuser1
        [password]=dbuser1
        [url]=jdbc:mysql://192.168.0.1:3306/db1
EOF
)
    db[${#db[@]}+1]=$(cat <<EOF
        [username]=dbuser2
        [password]=dbuser2
        [url]=jdbc:mysql://192.168.0.2:3307/db2
EOF
)
    declare -p db
    for i in ${!db[*]}
    do
        eval local -A config=(${db[$i]})   # 两次替换需要eval
        echo -n "   "
        declare -p config
        for k in ${!config[*]}
        do
            echo "      $k => ${config[$k]}"
        done
    done
}

main()
{
    local split="=================================================="
    echo $split

    index
    echo $split

    asso
    echo $split

    readin
    echo $split

    query
    echo $split

    split_join
    echo $split

    multi
    echo $split
}

main
exit $?
